package game

import (
	"GenshinImpact/server/src/conf"
	"fmt"
	"regexp"
	"time"
)

// 单例模式
var managerBanWord *ManagerBanWord

func GetManagerBanWord() *ManagerBanWord {
	if managerBanWord == nil {
		managerBanWord = new(ManagerBanWord)
		managerBanWord.BanWordBase = []string{"违禁词"}
		managerBanWord.BanWordExtra = []string{"原神"}
	}

	return managerBanWord
}

type ManagerBanWord struct {
	BanWordBase  []string // 配置导入
	BanWordExtra []string // 外部更新
}

// 验证字符串是否合法
func (self *ManagerBanWord) IsBanWord(text string) bool {
	for _, v := range self.BanWordBase {
		match, _ := regexp.MatchString(v, text)
		if match {
			return match
		}
	}

	for _, v := range self.BanWordExtra {
		match, _ := regexp.MatchString(v, text)
		if match {
			return match
		}
	}
	return false
}

// 启动违禁词库（定时调用更新）
func (self *ManagerBanWord) Run() {
	self.BanWordBase = conf.GetBanWordBase()
	ticker := time.NewTicker(time.Second * 10)
	for {
		select {
		case <-ticker.C:
			fmt.Println("词库已更新")
		}
	}
}
